package com.uh.rds.tester.standalone;

import com.uh.rds.testing.logger.TestLoggerFactory;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.slf4j.Logger;
import redis.clients.jedis.Connection;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

public class RedisClusterChecker {


    final static int RETRY_COUNT = 3;

    // Redis集群节点
    private final static HostAndPort CLUSTER_NODE;

    // 初始化集群节点
    static {
        CLUSTER_NODE = new HostAndPort("127.0.0.1", 6331);
    }

    // 连接池配置
    private static GenericObjectPoolConfig<Connection> getPoolConfig() {
        GenericObjectPoolConfig<Connection> poolConfig = new GenericObjectPoolConfig<Connection>();
        // 最大连接数
        poolConfig.setMaxTotal(100);
        // 最大空闲连接数
        poolConfig.setMaxIdle(20);
        // 最小空闲连接数
        poolConfig.setMinIdle(5);
        // 开启空闲资源监测
        poolConfig.setTestWhileIdle(true);
        return poolConfig;
    }

    public static void main(String[] args) {
        JedisCluster jedisCluster = null;
        try {
            // 创建JedisCluster实例，内部包含连接池
            jedisCluster = new JedisCluster(CLUSTER_NODE, 5000, 5000, 3,
                     getPoolConfig());


            for (int i = 0; i < 281500; i++) {
                String key = "memtier-" + i;
                try {
                    String actualValue = jedisCluster.get(key);
                    if (actualValue == null || actualValue.isEmpty()) {
                        System.out.println("Key: " + key + " not found or empty.");
                    }
                } catch (Exception e) {
                    System.out.println("Error getting key: " + key);
                    System.out.println("    " + e.getMessage());
                }
            }
            
        } finally {
            // 关闭连接
            if (jedisCluster != null) {
                try {
                    jedisCluster.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
